package com.baymax.test.scene.demo

import android.os.Bundle
import android.util.Log
import android.util.SparseIntArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.baymax.test.scene.BaseScene
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addText
import com.baymax.test.scene.newScrollContainer
import com.facebook.common.util.ByteConstants
import com.facebook.drawee.backends.pipeline.Fresco
import com.facebook.imagepipeline.core.ImagePipelineConfig
import com.facebook.imagepipeline.memory.*
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.internal.io.FileSystem
import okio.buffer
import java.io.File

/**
 * @title: 标题
 * @project: TestScene
 * @package: com.baymax.test.scene.demo
 * @class: MemoryScene
 * @description:
 *
 *
 * 类描述
 *
 *
 * @version: 1.0
 * @createDate: 2022/12/27 13:29
 * @Copyright (C) 2022 YSTEN
 * @author: xuyujian
 */
class MemoryScene : BaseScene() {

    val g = Gson()

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
        return p1.newScrollContainer {
            val txt = addText()
            addButton("calc memory") {
                val maxMem = Runtime.getRuntime().maxMemory()
                txt.append("max:$maxMem(${(maxMem /1024 /1024f)}m)")
                txt.append("\r\n")
                val total = Runtime.getRuntime().totalMemory()
                txt.append("total:$total(${(total /1024 /1024f)}m)")
                txt.append("\r\n")
            }

            addButton("cat file") {
                val b =
                    FileSystem.SYSTEM.source(File("ca")).buffer()

                b.use {
                    while (true){
                        val l = b.readUtf8Line()
                        println(l)
                        if (l == null){
                            break
                        }
                        txt.append(l)
                    }
                }
            }
            addButton("cat screen") {
                launch(Dispatchers.IO) {
                    println(requireActivity().windowManager.defaultDisplay.toString())
                }

            }



            addButton("init fresco") {
                val d = PoolParams(
                    0,
                    16 * ByteConstants.MB,
                    SparseIntArray(0)
                )
                Log.d(TAG, "fresco: ${g.toJson(d)}")

            }

        }
    }
}